package com.example.demo.controller;

import com.example.demo.annotation.OperSysLog;
import com.example.demo.common.code.PlatCodeStatement;
import com.example.demo.common.code.PlatStandardCode;
import com.example.demo.common.exception.LoginException;
import com.example.demo.dao.MpSysUserDao;
import com.example.demo.model.req.SysUserLoginReq;
import com.example.demo.model.req.SysUserRegisterReq;
import com.example.demo.pojo.MpSysUserDO;
import com.example.demo.service.FcSysLogService;
import com.example.demo.service.IUserService;
import com.example.demo.utils.IDCodeUtil;
import com.example.demo.utils.RespBean;
import com.example.demo.utils.TinyUUIDGenerate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/index")
@SuppressWarnings("all")
public class UserController {
    @Autowired
    MpSysUserDao mpSysUserDao;
    @Autowired
    IUserService iUserService;
    @Autowired
    FcSysLogService fcSysLogService;
    @OperSysLog(operModul = "登录模块",operDesc = "系统登录",operType = "查询")
    @PostMapping("/login")
    public RespBean login(@RequestBody SysUserLoginReq sysUserLoginReq,HttpServletRequest request,HttpServletResponse response) {
        log.info("{}进行登录",sysUserLoginReq);
        if(StringUtils.isEmpty(sysUserLoginReq.getSysUserName())){
            throw LoginException.build(PlatCodeStatement.SERVICE_PARAMS_MISSING);
        }
        return RespBean.success(iUserService.login(sysUserLoginReq, request, response));

    }

    @OperSysLog(operModul = "登录模块",operDesc = "系统注册",operType = "添加")
    @PostMapping("/register")
    public RespBean register(@RequestBody SysUserRegisterReq sysUserRegisterReq,
                             HttpServletRequest request,
                             HttpServletResponse response){
        log.info("{}进行登录",sysUserRegisterReq);
        if(StringUtils.isEmpty(sysUserRegisterReq.getSysUserName())){
            throw LoginException.build(PlatCodeStatement.SERVICE_PARAMS_MISSING);
        }
        return iUserService.register(sysUserRegisterReq, request, response);
    }

    @PostMapping("/show")
    public RespBean show(){
        return iUserService.showUser();
    }





    @OperSysLog(operModul = "登录模块",operType = "添加",operDesc = "测试功能")
    @RequestMapping("/test")
    @ResponseBody
    public RespBean test()
    {
        MpSysUserDO mpSysUserDO = new MpSysUserDO();
        mpSysUserDO.setCreateBy("曹玉琦");
        mpSysUserDO.setCreateTime(new Date());
        mpSysUserDO.setEmail("1654115063@QQ.com");
        mpSysUserDO.setSysUserId(TinyUUIDGenerate.generate());
        mpSysUserDO.setHeaderImage("1.jpg");
        mpSysUserDO.setCreateTime(new Date());
        mpSysUserDO.setJobNumber("zc10415");
        mpSysUserDO.setVersion(1);
        mpSysUserDO.setGender(0);
        mpSysUserDO.setIsEnable(0);
        mpSysUserDO.setIsDeleted(0);
        mpSysUserDao.insert(mpSysUserDO);
        return RespBean.success(mpSysUserDO);
    }
}
